package com.husd.framework.code;

import com.husd.framework.BaseLoadTest;
import com.husd.framework.ddl.DDL;
import org.apache.commons.lang3.StringUtils;

/**
 * @author shengdonghu@126.com
 * @date 2022/9/4 21:50
 */
public class VueCodeUtil extends BaseLoadTest {


    public String create(DDL ddl, String permission,
                         String nameCN, String nameEN) {

        String content = loadResourceString("auto_create_code/vue/vueIndex.vue");

        String queryForm = generateQueryForm(ddl);
        content = content.replaceAll("#_query_form_#", queryForm);

        // 替换权限控制字符
        content = content.replaceAll("#_permission_#", permission);

        // 标题
        content = content.replaceAll("#nameCN#", nameCN);
        content = content.replaceAll("#nameEN#", nameEN);

        // 分页的表格列
        String tableContent = generateTableContent(ddl);
        content = content.replaceAll("#_table_content_#", tableContent);

        // 更新功能列
        String updateFormContent = generateUpdateForm(ddl);
        content = content.replaceAll("#_update_form_#", updateFormContent);

        // 新增功能列
        String addFormContent = generateAddForm(ddl);
        content = content.replaceAll("#_add_form_#", addFormContent);


        return content;
    }

    private String generateUpdateForm(DDL ddl) {
        StringBuilder sb = new StringBuilder();
        //先生成查询的输入框
        final String searchFormName = "updateForm";
        for (DDLColumn ddlColumn : ddl.getColumnList()) {
            if (ignoreWithAddAndUpdate(ddlColumn.getColumnName())) {
                continue;
            }
            String formItemInputTemplate = loadResourceString("auto_create_code/vue/el_form_item_input.lst");
            formItemInputTemplate = formItemInputTemplate.replaceAll("#bind_form_name#", searchFormName);
            formItemInputTemplate = formItemInputTemplate.replaceAll("#columnNameCN#", ddlColumn.getComment());
            formItemInputTemplate = formItemInputTemplate.replaceAll("#columnNameEN#", ddlColumn.getColumnNameLowerCamel());
            sb.append(formItemInputTemplate);
        }
        return sb.toString();
    }

    private boolean ignoreWithAddAndUpdate(String columnName) {

        return StringUtils.equalsIgnoreCase("created_time", columnName) ||
                StringUtils.equalsIgnoreCase("created_by", columnName) ||
                StringUtils.equalsIgnoreCase("updated_time", columnName) ||
                StringUtils.equalsIgnoreCase("updated_by", columnName);
    }

    private boolean ignoreWithQuery(String columnName) {

        return StringUtils.equalsIgnoreCase("created_time", columnName) ||
                StringUtils.equalsIgnoreCase("updated_time", columnName);
    }

    private String generateAddForm(DDL ddl) {
        StringBuilder sb = new StringBuilder();
        //先生成查询的输入框
        final String searchFormName = "addForm";
        for (DDLColumn ddlColumn : ddl.getColumnList()) {
            if (ignoreWithAddAndUpdate(ddlColumn.getColumnName())) {
                continue;
            }
            String formItemInputTemplate = loadResourceString("auto_create_code/vue/el_form_item_input.lst");
            formItemInputTemplate = formItemInputTemplate.replaceAll("#bind_form_name#", searchFormName);
            formItemInputTemplate = formItemInputTemplate.replaceAll("#columnNameCN#", ddlColumn.getComment());
            formItemInputTemplate = formItemInputTemplate.replaceAll("#columnNameEN#", ddlColumn.getColumnNameLowerCamel());
            sb.append(formItemInputTemplate);
        }
        return sb.toString();
    }

    private String generateTableContent(DDL ddl) {

        StringBuilder sb = new StringBuilder();
        String t = "<el-table-column label=\"%s\" align=\"left\" prop=\"%s\" />\n";
        for (DDLColumn ddlColumn : ddl.getColumnList()) {
            String content = String.format(t, ddlColumn.getComment(), ddlColumn.getColumnNameLowerCamel());
            sb.append(content);
        }
        return sb.toString();
    }

    private String generateQueryForm(DDL ddl) {

        StringBuilder sb = new StringBuilder();
        //先生成查询的输入框
        final String searchFormName = "queryParams";
        String sep = "</el-row> <el-row :gutter=\"0\">";
        sb.append("<el-row :gutter=\"0\">\n");
        for (DDLColumn ddlColumn : ddl.getColumnList()) {
            if (ignoreWithQuery(ddlColumn.getColumnName())) {
                continue;
            }
            sb.append("<el-col :span=\"6\">\n");
            String formItemInputTemplate = loadResourceString("auto_create_code/vue/el_form_item_input.lst");
            formItemInputTemplate = formItemInputTemplate.replaceAll("#bind_form_name#", searchFormName);
            formItemInputTemplate = formItemInputTemplate.replaceAll("#columnNameCN#", ddlColumn.getComment());
            formItemInputTemplate = formItemInputTemplate.replaceAll("#columnNameEN#", ddlColumn.getColumnNameLowerCamel());
            sb.append(formItemInputTemplate);
            sb.append("</el-col>\n");
        }
        sb.append(" </el-row>\n");
        return sb.toString();
    }

    public String generateJsFile(String nameEN,String requestMapping) {

        String content = loadResourceString("auto_create_code/vue/vuejs.js");

        content = content.replaceAll("#nameEN#", nameEN);
        content = content.replaceAll("#requestMapping#", requestMapping);

        return content;
    }
}
